<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-divider content-position="left">设备基本信息</el-divider>
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="设备类型" prop="deviceTypeId">
						<el-select v-model="form.deviceTypeId" filterable placeholder="请选择设备类型">
							<el-option v-for="item in deviceTypeList" :key="item.id" :value="item.id" :label="item.typeName" :disabled="item.state === 20" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="设备型号" prop="deviceModelName">
						<el-input v-model="form.deviceModelName" placeholder="请输入设备型号" maxlength="50" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="通讯模型" prop="communicationModelId">
						<el-select v-model="form.communicationModelId" filterable placeholder="请选择通讯模型">
							<el-option v-for="item in communicateList" :key="item.id" :value="item.id" :label="item.name" :disabled="item.state === 20" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="运营商" prop="operatorId">
						<el-select v-model="form.operatorId" filterable placeholder="请选择运营商">
							<el-option v-for="item in operatorList" :key="item.id" :value="item.id" :label="item.name" :disabled="item.state === 20" />
						</el-select>
					</el-form-item>
				</el-col>
				<!-- <el-divider content-position="left">设备拓展信息</el-divider>
				<el-col :span="12" class="mb20">
					<el-form-item label="品牌" prop="operatorId">
						<el-input v-model="form.brand" placeholder="请输入品牌" maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="检测方式" prop="operatorId">
						<el-input v-model="form.deviceModelName" placeholder="请输入检测方式" maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="生产商" prop="operatorId">
						<el-input v-model="form.deviceModelName" placeholder="请输入生产商" maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="量程" prop="operatorId">
						<el-input v-model="form.deviceModelName" placeholder="请输入量程" maxlength="50" />
					</el-form-item>
				</el-col> -->
				<el-divider content-position="left">设备图片</el-divider>
				<el-col :span="12" class="mb20">
					<el-form-item label="设备图片" prop="deviceImage">
						<ImageUpload v-model:imageUrl="form.deviceImage"> </ImageUpload>
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="设备证书" prop="deviceCertificate">
						<ImageUpload v-model:imageUrl="form.deviceCertificate"> </ImageUpload>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicDeviceModelDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/basicDeviceModel';
import { fetchListAll } from '/@/api/platform/basicDeviceType';
import { fetchListAll as fetchCommunicateList } from '/@/api/platform/basicCommunicationModel';
import { fetchListAll as fetchOperatorList } from '/@/api/platform/basicOperatorType';
import { rule } from '/@/utils/validate';
const ImageUpload = defineAsyncComponent(() => import('/@/components/Upload/Image.vue'));
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	deviceTypeId: '',
	deviceModelName: '',
	communicationModelId: '',
	operatorId: '',
	state: 10,
	// deptId: '',
	// brand: '',
	// detectionMethod: '',
	// producer: '',
	// range: '',
	deviceImage: '',
	deviceCertificate: '',
	sort: 0,
});
const deviceTypeList: any = ref([]);
const communicateList: any = ref([]);
const operatorList: any = ref([]);

// 定义校验规则
const dataRules = ref({
	deviceTypeId: [{ required: true, message: '设备类型不能为空', trigger: 'blur' }],
	deviceModelName: [
		{ required: true, message: '设备型号不能为空', trigger: 'blur' },
		{ validator: rule.checkSpaceInline, trigger: 'blur' },
	],
	communicationModelId: [{ required: true, message: '通讯模型不能为空', trigger: 'blur' }],
	operatorId: [{ required: true, message: '运营商不能为空', trigger: 'blur' }],
	// deptId: [{ required: true, message: '授权单位不能为空', trigger: 'blur' }],
	// brand: [{ required: true, message: '品牌不能为空', trigger: 'blur' }],
	// detectionMethod: [{ required: true, message: '检测方式不能为空', trigger: 'blur' }],
	// producer: [{ required: true, message: '生产商不能为空', trigger: 'blur' }],
	// range: [{ required: true, message: '量程不能为空', trigger: 'blur' }],
	// deviceImage: [{ required: true, message: '设备图片不能为空', trigger: 'blur' }],
	// deviceCertificate: [{ required: true, message: '设备证书不能为空', trigger: 'blur' }],
	// sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
});

// 获取所有设备类型
const getDeviceTypes = () => {
	fetchListAll({ state: 10 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
// 获取通讯模型
const getCommunicateList = () => {
	fetchCommunicateList({ state: 20 }).then((res) => {
		communicateList.value = res.data;
	});
};
const getOperatorList = () => {
	fetchOperatorList({ state: 20 }).then((res) => {
		operatorList.value = res.data;
	});
};
// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	getDeviceTypes();
	getCommunicateList();
	getOperatorList();
	// 获取basicDeviceModel信息
	if (id) {
		form.id = id;
		getbasicDeviceModelData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicDeviceModelData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
